package com.vvlive.caibao.smartcity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.FunctionEditable;
import com.vvlive.caibao.smartcity.bean.IndexFuncationListBean;

import org.askerov.dynamicgrid.BaseDynamicGridAdapter;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by lenovo on 2015/6/29.
 */
public class IndexFunctionAdapter extends BaseDynamicGridAdapter {

    private List<IndexFuncationListBean> mList;
    private boolean mIsEditable = false;
    private FunctionEditable mFunctionEditable;

    public IndexFunctionAdapter(Context context, FunctionEditable functionEditable, List<IndexFuncationListBean> mList) {
        super(context, mList, 4);
        this.mList = mList;
        this.mFunctionEditable = functionEditable;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.index_gv_item, null);
            holder = new ViewHolder();
            holder.mImageView = (ImageView) convertView.findViewById(R.id.index_item_icn);
            holder.mTextView = (TextView) convertView.findViewById(R.id.index_item_text);
            holder.mAdd = (ImageView) convertView.findViewById(R.id.index_item_add);
            holder.mFuncDelete = (TextView) convertView.findViewById(R.id.func_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        if (position == mList.size() - 1) {
//            holder.mAdd.setImageResource(mList.get(position).getIcn());
//            holder.mFuncDelete.setVisibility(View.GONE);
//        } else {
        holder.mImageView.setImageResource(mList.get(position).getIcn());
        holder.mTextView.setText(mList.get(position).getText());
        TextView funcDelete = holder.mFuncDelete;
        funcDelete.setVisibility(mIsEditable ? View.VISIBLE : View.GONE);
        funcDelete.setTag(position);
        funcDelete.setOnClickListener(mClickListener);
//        }

//        holder.mFuncDelete.setVisibility(View.GONE);

        return convertView;
    }

    public void setEditable(boolean isEditable) {
        mIsEditable = isEditable;
        this.notifyDataSetChanged();
    }

    static class ViewHolder {
        public ImageView mAdd;
        public ImageView mImageView;
        public TextView mTextView;
        public TextView mFuncDelete;
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            for (int i = 0, size = mList.size(); i < size; i++) {
                if (position == i) {
                    Object obj = mList.get(i);
                    IndexFunctionAdapter.super.remove(obj);
                    mList.remove(obj);
                    mFunctionEditable.onDelete(obj, position);
                    break;
                }
            }
        }
    };
}
